package com.sgcc.fj.pmos.e_fj_home.presenter;

import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.QueryPriceTrendBean;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.e_fj_home.bean.MarketSituationBean;
import com.sgcc.fj.pmos.e_fj_home.bean.NoTimeDivisionPriceTrendBean;
import com.sgcc.fj.pmos.e_fj_home.bean.TimeDivisionPriceTrendBean;
import com.sgcc.fj.pmos.e_fj_home.contract.MarketSituationContract;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class MarketSituationPresenter extends BasePresenter<MarketSituationContract.View> implements MarketSituationContract.Presenter {

    @Override
    public void getData() {
        MarketSituationBean marketSituationBean = new MarketSituationBean();
        getView().querySuccess(marketSituationBean);

    }

    /**
     * 市场均价
     */
    @Override
    public void queryPriceTrend() {
        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_PRICE_TREND,
                new MvpNetCallback<QueryPriceTrendBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(QueryPriceTrendBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response && null != response.getData())
                            getView().queryPriceTrendSuccess(response.getData());
                    }
                });


    }

    /**
     * 分时价格浮动项趋势
     */
    @Override
    public void queryTimeDivisionPriceTrend(String startDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("_t", System.currentTimeMillis());

        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_TIME_DIVISION_PRICE_TREND,
                new MvpNetCallback<TimeDivisionPriceTrendBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(TimeDivisionPriceTrendBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response && null != response.getData())
                            getView().queryTimeDivisionPriceTrendSuccess(response.getData());
                    }
                });

    }

    /**
     * 不分时价格浮动项趋势
     */
    @Override
    public void queryNoTimeDivisionPriceTrend(String endDate, String startDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.QUERY_NO_TIME_DIVISION_PRICE_TREND,
                new MvpNetCallback<NoTimeDivisionPriceTrendBean>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().queryFailed();
                    }

                    @Override
                    public void onResponse(NoTimeDivisionPriceTrendBean response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response && null != response.getData())
                            getView().queryNoTimeDivisionPriceTrendSuccess(response.getData());
                    }
                });

    }
}
